{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-29T01:27:17.287867Z",
     "start_time": "2025-07-29T01:27:15.496557Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:36:12.687769Z",
     "start_time": "2025-07-29T01:36:12.673548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "案例1:学生成绩分析场景:某班级的学生成绩数据如下，请完成以下任务:\n",
    "1.计算每位学生的总分和平均分。\n",
    "2.找出数学成绩高于90分或英语成绩高于85分的学生。\n",
    "3.按总分从高到低排序，并输出前3名学生\n",
    "\"\"\"\n",
    "\n",
    "date = {\n",
    "    '姓名': ['张三', '李四', '王五', '赵六', '钱七'],\n",
    "    '数学': [85, 92, 78, 88, 95],\n",
    "    '英语': [90, 88, 85, 92, 80],\n",
    "    '物理': [75, 80, 88, 85, 90]\n",
    "}\n",
    "\n",
    "score = pd.DataFrame(date)\n",
    "\n",
    "score['总分'] = score['数学'] + score['英语'] + score['物理']\n",
    "\n",
    "score[(score['数学'] > 90) | (score['英语'] > 85)].sort_values('总分', ascending=False).head(3)\n"
   ],
   "id": "b31a4d0a67badb43",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   姓名  数学  英语  物理   总分\n",
       "3  赵六  88  92  85  265\n",
       "4  钱七  95  80  90  265\n",
       "1  李四  92  88  80  260"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>物理</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>88</td>\n",
       "      <td>92</td>\n",
       "      <td>85</td>\n",
       "      <td>265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>钱七</td>\n",
       "      <td>95</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "      <td>80</td>\n",
       "      <td>260</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:42:24.463070Z",
     "start_time": "2025-07-29T01:42:24.441111Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "案例2:销售数据分析场景:某公司销售数据如下，请完成以下任务:\n",
    "1.计算每种产品的总销售额(销售额 = 单价 x 销量)\n",
    "2.找出销售额最高的产品。\n",
    "按销售额从高到低排序，并输出所有产品信息了.\n",
    "\"\"\"\n",
    "date = {\n",
    "    'name': ['A', 'B', 'C', 'D'],\n",
    "    '单价': [100, 150, 200, 120],\n",
    "    '销量': [50, 30, 20, 40]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(date)\n",
    "\n",
    "\n",
    "df['销售额'] = df['单价'] * df['销量']\n",
    "\n",
    "df.nlargest(1, '销售额')\n",
    "\n",
    "df.sort_values('销售额', ascending=False)\n"
   ],
   "id": "ef280a5865070e76",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  name   单价  销量   销售额\n",
       "0    A  100  50  5000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>单价</th>\n",
       "      <th>销量</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>100</td>\n",
       "      <td>50</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
